package com.example.shirodemo.utils;

import cn.hutool.crypto.SecureUtil;
import java.util.Random;

/**
 * @author 绫小路
 * @date 2021/1/13 21:26
 * @description
 */
public class CommonUtils {

  /**
   * 根据传入长度，返回随机盐
   *
   * @param length
   * @return
   */
  public static String getSalt(int length) {
    String code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*-+/[]()@#$%&!";
    char[] codes = code.toCharArray();
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < length; i++) {
      result.append(codes[new Random().nextInt(codes.length)]);
    }
    return result.toString();
  }

/*  public static void main(String[] args) {
    System.out.println(getSalt(4));
  }*/


  /**
   * 根据传入密码和盐返回MD5加密
   * @param password
   * @param salt
   * @return
   */
  public static String passwordToMD5(String password, String salt) {
    StringBuffer pwd = new StringBuffer(password);
    pwd.insert(1, salt);
    return SecureUtil.md5(pwd.toString());
  }
}
